package me.chanjar.weixin.mp.util.json;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import me.chanjar.weixin.mp.bean.card.WxMpCardBaseInfo;
import me.chanjar.weixin.mp.bean.card.WxMpCardCreateParam;
import me.chanjar.weixin.mp.bean.card.WxMpCardDateInfo;
import me.chanjar.weixin.mp.bean.card.WxMpCardModel;

/**
 * 
 * @author francis
 * @version 18/03/27
 */
public class WxMpCardCreateGsonAdapter implements JsonSerializer<WxMpCardCreateParam> {

	public JsonElement serialize(WxMpCardCreateParam src, Type typeOfSrc, JsonSerializationContext context) {
		
		Map<Object,Object> all = new LinkedHashMap<>();
		Map<Object,Object> card = new LinkedHashMap<>();
		Map<Object,Object> type = new LinkedHashMap<>();
		
		WxMpCardModel model = src.getModel();
		type.put("base_info",model.getBaseInfo());
		type.put("advanced_info",model.getAdvancedInfo());
		
		card.put("card_type", src.getCardType());
		card.put(src.getCardType().toLowerCase(), type);
		if(src.getCardType().equalsIgnoreCase("GROUPON")) {
			type.put("deal_detail", model.getDealDetail());
		}else if(src.getCardType().equalsIgnoreCase("CASH")) {
			type.put("least_cost", model.getLeastCost());
			type.put("reduce_cost", model.getReduceCost());
		}else if(src.getCardType().equalsIgnoreCase("DISCOUNT")) {
			type.put("discount", model.getDiscount());
		}else if(src.getCardType().equalsIgnoreCase("GIFT")) {
			type.put("gift", model.getGift());
		}else if(src.getCardType().equalsIgnoreCase("GENERAL_COUPON")) {
			type.put("default_detail", model.getDefaultDetail());
		}
		all.put("card", card);
		String jsonStr = new Gson().toJson(all);
		JsonParser jsonParser = new JsonParser();
		JsonElement jsonElement = jsonParser.parse(jsonStr);
		return jsonElement;
	}
	
	/**
	 * demo
	 * @param args
	 */
	public static void main(String[] args) {
		WxMpCardCreateParam src = new WxMpCardCreateParam();
		src.setCardType("CASH");
		WxMpCardModel model = new WxMpCardModel();
		WxMpCardBaseInfo info = new WxMpCardBaseInfo();
		info.setActivateAppBrandUserName("asdqad@app");
		List<Integer> list = new ArrayList<>();
		list.add(1);
		list.add(2);
		info.setLocationIdList(list);
		WxMpCardDateInfo dateInfo = new WxMpCardDateInfo();
		dateInfo.setType("1");
		dateInfo.setBeginTimestamp(121315454L);
		info.setDateInfo(dateInfo);
		model.setBaseInfo(info);
		model.setDealDetail("asdasasd");
		model.setLeastCost(100);
		src.setModel(model);
		String json = src.toJson();
		System.out.println(json);
	}

}
